package com.game.doudizhu.core;

import com.game.doudizhu.model.Card;
import java.util.*;
import java.util.stream.Collectors;

public class CardUtil {
    // 获取一组牌中的最大权重
    public static int getMaxWeight(List<Card> cards) {
        if (cards == null || cards.isEmpty()) {
            return 0;
        }
        return cards.stream()
                .mapToInt(Card::getWeight)
                .max()
                .orElse(0);
    }
    
    // 检查是否有王炸
    public static boolean hasRocket(List<Card> cards) {
        boolean hasSmallJoker = false;
        boolean hasBigJoker = false;
        for (Card card : cards) {
            if (card.getSuit() == Card.Suit.JOKER) {
                if (card.getRank().equals("SMALL")) hasSmallJoker = true;
                if (card.getRank().equals("BIG")) hasBigJoker = true;
            }
        }
        return hasSmallJoker && hasBigJoker;
    }
    
    // 寻找王炸
    public static List<Card> findRocket(List<Card> cards) {
        return cards.stream()
                .filter(card -> card.getSuit() == Card.Suit.JOKER)
                .collect(Collectors.toList());
    }
    
    // 寻找所有炸弹
    public static List<List<Card>> findBombs(List<Card> cards) {
        return cards.stream()
                .collect(Collectors.groupingBy(Card::getRank))
                .values()
                .stream()
                .filter(list -> list.size() == 4)
                .collect(Collectors.toList());
    }
    
    // 寻找所有对子
    public static List<List<Card>> findPairs(List<Card> cards) {
        return cards.stream()
                .collect(Collectors.groupingBy(Card::getRank))
                .values()
                .stream()
                .filter(list -> list.size() >= 2)
                .map(list -> list.subList(0, 2))
                .collect(Collectors.toList());
    }
    
    // 寻找所有三张
    public static List<List<Card>> findTriples(List<Card> cards) {
        return cards.stream()
                .collect(Collectors.groupingBy(Card::getRank))
                .values()
                .stream()
                .filter(list -> list.size() >= 3)
                .map(list -> list.subList(0, 3))
                .collect(Collectors.toList());
    }
    
    // 找到最小的牌
    public static Card findSmallestCard(List<Card> cards) {
        return Collections.min(cards, Comparator.comparingInt(Card::getWeight));
    }
    
    // 统计牌型分组
    public static Map<String, List<Card>> groupByRank(List<Card> cards) {
        return cards.stream()
                .collect(Collectors.groupingBy(Card::getRank));
    }
} 